package com.hanbang.chu_xing_gao_zhao.adapter;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.hanbang.chu_xing_gao_zhao.application.MyApplication;
import com.hanbang.chu_xing_gao_zhao.entity.BannerAdData;
import com.hanbang.chu_xing_gao_zhao.utils.BitmapUtil;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.BitmapUtils;

public class BannerAdAdapter extends PagerAdapter implements OnPageChangeListener {

	private Context context = null;
	private ArrayList<ImageView> list = new ArrayList<ImageView>();
	private ArrayList<BannerAdData> data = null;
	private RadioGroup dotLayout = null;
	private BitmapUtils bitmapUtils = null;
	private ViewPager viewPager = null;
	private int time = 1000 * 3;
	private OnItemClickListener onItemClickListener = null;

	public BannerAdAdapter(Context context, RelativeLayout root, ArrayList<BannerAdData> data) {
		bitmapUtils = BitmapUtil.getBitmapUtils(context, MyApplication.bitmapCachePath);
		this.data = data;
		this.context = context;
		this.dotLayout = (RadioGroup) ((LinearLayout) root.getChildAt(1)).getChildAt(0);
		this.viewPager = (ViewPager) root.getChildAt(0);
		viewPager.setAdapter(this);
		// pager翻页
		viewPager.setOnPageChangeListener(this);
		if (data.size() > 0) {
			notifyDataSetChanged();
		}
		setWidthAndHeight(1, 1);
	}

	/*
	 * 设置宽高比 16：9 默认16：9
	 */
	@SuppressWarnings("deprecation")
	public void setWidthAndHeight(int width, int height) {
		ViewGroup.LayoutParams params = viewPager.getLayoutParams();
		params.height = (int) Math.rint(((Activity) context).getWindowManager().getDefaultDisplay().getWidth() * height
				/ (width * 1.0));
		viewPager.setLayoutParams(params);
	}

	private Timer timer = null;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 123) {
				if (data.size() > 1) {
					viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
				}
			}
		}
	};

	public int getCurrentItem() {
		return viewPager.getCurrentItem();
	}

	public ImageView getCurrentItemView() {
		return list.get(viewPager.getCurrentItem());
	}

	public void start() {
		stop();
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				handler.sendEmptyMessage(123);
			}
		}, 1000, time);
	}

	public void stop() {
		if (timer != null) {
			timer.cancel();
		}
	}

	@Override
	public void notifyDataSetChanged() {
		LayoutParams params = new LayoutParams();
		params.width = LayoutParams.MATCH_PARENT;
		params.height = LayoutParams.MATCH_PARENT;
		RadioGroup.LayoutParams params2 = new RadioGroup.LayoutParams(SystemTools.dip2px(context, 8),
				SystemTools.dip2px(context, 8));
		params2.setMargins(0, 0, SystemTools.dip2px(context, 4), 0);
		dotLayout.removeAllViews();
		list.clear();
		for (int i = 0; i < data.size(); i++) {
			// 设置图片
			ImageView image = new ImageView(context);
			image.setScaleType(ScaleType.FIT_XY);
			image.setLayoutParams(params);
			list.add(image);
			image.setTag(data.get(i));
			image.setOnClickListener(new MyOnClickListener(i));
			// 设置圆点
			RadioButton view = new RadioButton(context);
			view.setLayoutParams(params2);
			view.setBackgroundResource(R.drawable.dot_focus);
			view.setButtonDrawable(R.drawable.dot_focus);
			view.setClickable(false);
			dotLayout.addView(view);
		}
		if (list.size() > 1) {
			// 设置图片
			ImageView image = new ImageView(context);
			image.setScaleType(ScaleType.FIT_XY);
			image.setLayoutParams(params);
			list.add(0, image);
			// 设置图片
			image = new ImageView(context);
			image.setScaleType(ScaleType.FIT_XY);
			image.setLayoutParams(params);
			list.add(image);
		}
		super.notifyDataSetChanged();
		if (list.size() != 0) {
			if (list.size() % 2 == 0) {
				viewPager.setCurrentItem(list.size() / 2 - 1, false);
				onPageSelected(viewPager.getCurrentItem());
			} else {
				viewPager.setCurrentItem(list.size() / 2, false);
				onPageSelected(viewPager.getCurrentItem());
			}
		}
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return (arg0 == arg1);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		position = position % list.size();
		container.removeView(list.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		position = position % list.size();
		ImageView image = list.get(position);
		String uri = "";

		if (position == list.size() - 1) {
			uri = SystemTools.getHttpFileUrl(data.get(0).getPath());
		} else if (position == 0) {
			uri = SystemTools.getHttpFileUrl(data.get(data.size() - 1).getPath());
		} else {
			uri = SystemTools.getHttpFileUrl(data.get(position - 1).getPath());
		}
		bitmapUtils.display(image, uri);
		container.addView(list.get(position));// 添加页卡
		return list.get(position);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		if (list.size() > 1) {
			if (arg0 == list.size() - 1) {
				viewPager.setCurrentItem(1, false);
			} else if (arg0 == 0) {
				viewPager.setCurrentItem(list.size() - 2, false);
			} else if (arg0 > 0) {
				((RadioButton) dotLayout.getChildAt(arg0 - 1)).setChecked(true);
			}
		} else {
			((RadioButton) dotLayout.getChildAt(0)).setChecked(true);
		}
	}

	private class MyOnClickListener implements OnClickListener {

		int position = -1;

		public MyOnClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			if (onItemClickListener != null) {
				onItemClickListener.onItemClick(v, position);
			}
		}

	}

	public interface OnItemClickListener {
		public void onItemClick(View v, int position);
	}

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}
}
